#include "matrix_proxy.hpp"
#include "tmp_file.hpp"
#include <sstream>
#include <iostream>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <cstdlib>
#include "stopwatch.hpp"

void generate(){
    int32_t sizes[2];
    sizes[0] = 9;
    sizes[1] = 6;

    std::vector<float> data;

    float a = 0;
    for( int i = 0; i < sizes[1]; ++i ){
        for( int j = 0; j < sizes[0]; ++j ){
            data.push_back(a);
            a += 1;
        }
    }
    FILE * f = fopen("small_matrix.in","w");
    if (!f)
        return;

    fwrite( sizes, 4, 2, f );
    fwrite( &data.front(), sizeof(float), data.size(), f);
    fclose( f );
}







static void usage(){
    std::cout << "usage: \nprogram_name filename_matrix_1 filename_matrix_2 filename_matrix_result" << std::endl;
}

void trial(){

    //generate();
    MatrixProxy m1( "small_matrix.in");
    std::cout << m1.m() << " x " << m1.n() << std::endl;
    std::cout << std::endl;

    m1.print();
    std::cout << std::endl;
    m1.print2();

    size_t n = m1.n();
    size_t m = m1.m();
    for( size_t i = 0; i < m; i += 3){
        for(size_t j = 0; j < n; j += 3){
            size_t t = 0;
            m1.read( i, j, 3, 3 );
            for( int k = 0; k < 3; ++k){
                for( int l = 0; l < 3; ++l){
                    std::cout << std::right << std::setw( 5 ) << std::fixed << std::setprecision(1) << m1.buffer()[t] << " ";
                    ++t;
                }
                std::cout << std::endl;
            }
            std::cout << std::endl;
        }
    }

}

int main(int argc, char *argv[]){
    try{
        //MatrixProxy m1( "matrix1.in");
        //std::cout << m1.m() << " x " << m1.n() << std::endl;
        //MatrixProxy m2( "matrix2.in");
        //std::cout << m2.m() << " x " << m2.n() << std::endl;
        if ( argc < 4 ){
            usage();
            return 1;
        }
        MatrixProxy m1(argv[1]);
        std::cout << m1.m() << " x " << m1.n() << std::endl;
        size_t size = m1.n() * m1.m() * sizeof(float) + 2 * sizeof(int32_t);
        TmpFile tmpFile(size);

        MatrixProxy m2(argv[2]);
        std::cout << m2.m() << " x " << m2.n() << std::endl;

        return 0;

        trial();




    }catch( std::exception & e ){
        std::cout << e.what();
    }

}
